package user;

import book.Book;
import book.Library;
import book.PairofUidAndBookId;
import constant.Constant;
import utils.AnalyzingBook;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-03
 * Time: 17:26
 */
public class NormalUser extends User{
    public Scanner scanner;
    private int borrowcount; //当前书籍借阅量
    private Library library;
    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
    private PairofUidAndBookId[] pairofUidAndBookIds; //存储借阅图书信息

    public NormalUser(String name, int userID){
        super(name, userID, "普通用户");
        scanner = ScannerSingleton.getScannerSingleton();
        library = Library.getLibrary();
    }

    @Override
    public int display() {
        System.out.println("普通用户 " + this.getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个人借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    private void loadBorrowedBook(){
        try{
            PairofUidAndBookId[] allBorrowedBook;
            allBorrowedBook = analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
            pairofUidAndBookIds = new PairofUidAndBookId[Constant.BORROW_BOOK_MAX_NUM];
            //如果没有读取的借阅信息
            if(allBorrowedBook == null){
                borrowcount = 0;
            }else{
                int allborrowedCount = allBorrowedBook.length;  //查看实际读取到的数组长度是多少
                if(allborrowedCount > pairofUidAndBookIds.length){  //如果读取到10本书被借阅，但是借阅信息数组小于读取的
                    pairofUidAndBookIds = new PairofUidAndBookId[allborrowedCount];
                }
                //7.把数据拷贝回到 已借阅图书信息的数组当中
                for (int i = 0; i < allborrowedCount; i++) {
                    pairofUidAndBookIds[i] = allBorrowedBook[i];
                }
                //8.更新当前实际借阅书籍的书籍数量
                borrowcount = allborrowedCount;
            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
    private void storeBorrowedBook() {
        try {
            analyzingBorrowedBook.storeObject(pairofUidAndBookIds, Constant.BORROWED_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //借阅图书
    public void borrowBook() {
        scanner.nextLine(); //防止输入数据换行时，错误读取到换行
        System.out.println("请输入你要借阅图书的书籍ID：");

        int bookId = scanner.nextInt();
        scanner.nextLine();
        if(library.bookCount == 0){
            System.out.println("图书馆没有书籍可以借阅....");
            return;
        }
        loadBorrowedBook();
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("没有你借阅的图书，书籍ID为："+bookId);
            return;
        }
        //检查借阅信息里面是否有这本书，如果有，那么这本书已经被借出
        for (int i = 0; i < borrowcount ; i++) {
            PairofUidAndBookId pairofUidAndBookId = pairofUidAndBookIds[i];
            if(pairofUidAndBookId.getBookId() == book.getBookId()){ //借阅信息里面有这本书，说明这本书已经被借阅
                if(pairofUidAndBookId.getUserId() == getUserID()){
                    System.out.println("这本书是你自己已经借阅过的！");
                    return;
                }else{
                    System.out.println("这本书是别人已经借阅过的！");
                    return;
                }
            }
        }
        library.borrowBook(bookId); //到这里，说明这本书没有被借出
        PairofUidAndBookId pairofUidAndBookId = new PairofUidAndBookId(getUserID(), book.getBookId());
        pairofUidAndBookIds[borrowcount] = pairofUidAndBookId;
        borrowcount++;
        storeBorrowedBook();
        System.out.println("借阅书籍成功！！！");
    }

    public void returnBook() {
        loadBorrowedBook();
        if(borrowcount == 0){
            System.out.println("目前没有用户借阅过书籍");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入你要归还图书的id：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("没有该id的相关书籍："+bookId);
            return;
        }
        for (int i = 0; i < borrowcount; i++) {
            if(pairofUidAndBookIds[i].getBookId() == book.getBookId()){     //这本书确实被借阅了
                library.returnBook(bookId);
                System.out.println("图书 '" + book.getTitle() + "' 已成功归还。");
                // 用最后一本替换归还的书
                pairofUidAndBookIds[i] = pairofUidAndBookIds[borrowcount - 1];
                // 清空最后一个
                pairofUidAndBookIds[borrowcount - 1] = null;
                borrowcount--;
                storeBorrowedBook();
            }else{
                System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getTitle());
            }
        }
    }

    public void viewBorrowHistory() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadBorrowedBook();

        System.out.println("您的借阅情况如下：");
        if(borrowcount == 0){
            System.out.println("目前没有借阅记录.....");
        }else{
            boolean flag = false;
            for (int i = 0; i < borrowcount; i++) {
                if(pairofUidAndBookIds[i].getUserId() == getUserID()){
                    //这里只能查看属于自己借阅的情况
                    //用户ID相同的情况下，使用书籍ID查询书籍
                    flag = true;
                    Book book = library.searchById(pairofUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flag) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }
}
